home *** CD-ROM | disk | FTP | other *** search
- var gic2 = {
-
- id: 2,
- version: 1.1,
- page: 'google-analytics-firefox-extension',
- name: 'Google Analytics Watcher',
-
- prefs: null,
- token: '',
- profiles: '',
- profile_last: 0,
- profile_set_menu: false,
-
- startup: function()
- {
- // Name
- this.gebi('label').label = this.name;
-
- // Preferences
- this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch('extensions.gic'+this.id+'.');
- this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
- this.prefs.addObserver('', this, false);
-
- // Install & update
- if (this.prefs.getIntPref('first'))
- {
- this.prefs.setIntPref('first', 0);
- setTimeout(this.web, 500, '/'+this.page+'/?mode=welcome');
- }
- var pref_version = this.prefs.getCharPref('version');
- if (pref_version < this.version)
- {
- this.prefs.setCharPref('version', this.version);
- setTimeout(this.web, 500, '/'+this.page+'/?mode=update&from='+pref_version+'&to='+this.version);
- }
-
- this.token = this.prefs.getCharPref('token');
- this.profiles = this.prefs.getCharPref('profiles');
- this.profile_last = this.prefs.getIntPref('profile_last');
-
- this.profiles_menu();
-
- if (!this.cache()) this.main();
- },
-
- auth: function()
- {
- gic2.prefs.setCharPref('token', '');
-
- var pref_email = this.prefs.getCharPref('email');
- var pref_password = this.prefs.getCharPref('password');
-
- if (!pref_email || !pref_password)
- {
- gic2.gebi('label').style.color = '#000';
- return;
- }
-
- var ajax = null;
- ajax = new XMLHttpRequest();
- ajax.open('POST', 'https://www.google.com/accounts/ClientLogin'+'?'+(new Date()).getTime(), true);
- ajax.setRequestHeader('GData-Version', '2');
- ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- ajax.onload = function()
- {
- gic2.gebi('label').style.color = '#000';
-
- if (ajax.status == 200)
- {
- var regexp = /^Auth=.+$/im;
- var matches = ajax.responseText.match(regexp);
- if (!matches[0])
- {
- gic2.gebi('label').label = 'Google syntax error.';
- }
- else
- {
- gic2.token = matches[0];
- gic2.prefs.setCharPref('token', gic2.token);
- }
- }
- else
- {
- gic2.gebi('label').label = 'Error #'+ajax.status+' '+ajax.responseText;
- }
- };
- ajax.send('accountType=GOOGLE&Email='+pref_email+'&Passwd='+pref_password+'&service=analytics&source=gic-gic'+this.id+'-'+this.version);
- },
-
- accounts: function()
- {
- if (!this.token) this.auth();
- if (!this.token) return;
-
- this.prefs.setCharPref('profiles', '');
-
- var ajax = null;
- ajax = new XMLHttpRequest();
- ajax.open('GET', 'https://www.google.com/analytics/feeds/accounts/default?prettyprint=true'+'&'+(new Date()).getTime(), true);
- ajax.setRequestHeader('Authorization', 'GoogleLogin '+this.token);
- ajax.setRequestHeader('GData-Version', '2');
- ajax.onload = function()
- {
- gic2.gebi('label').style.color = '#000';
-
- if (ajax.status == 200)
- {
- var xml = ajax.responseXML;
-
- try
- {
- var entry = xml.getElementsByTagName('entry');
- var profiles = [];
- for (var i=0; i<entry.length; i++)
- {
- profiles[i] = entry[i].getElementsByTagName('title').item(0).firstChild.data+'|'+entry[i].getElementsByTagName('dxp:tableId').item(0).firstChild.data;
- }
- gic2.profiles = profiles.join('||');
- }
- catch(e) {}
-
- if (gic2.profiles)
- {
- gic2.prefs.setCharPref('profiles', gic2.profiles);
- }
- else
- {
- gic2.gebi('label').label = 'Error: there are no profiles in your account.';
- }
- }
- else
- {
- // Invalid token
- if (ajax.status == 401)
- {
- gic2.auth();
- gic2.accounts();
- }
- else
- {
- gic2.gebi('label').label = 'Error #'+ajax.status;
- }
- }
- };
- ajax.send(null);
- },
-
- main: function()
- {
- this.gebi('label').style.color = '#FFF';
-
- if (!this.token) this.auth();
- if (!this.token) return;
-
- if (!this.profiles) this.accounts();
- if (!this.profiles) return;
-
- // Direct click
- if (this.profile_set_menu)
- {
- this.profile_set_menu = false;
- }
- else
- {
- // Switch profiles on click
- if (this.prefs.getBoolPref('profile_switch_click'))
- {
- this.profile_last++;
- this.prefs.setIntPref('profile_last', this.profile_last);
- }
- }
-
- var profiles = this.profiles.split('||');
-
- // In array
- if (this.profile_last > (profiles.length - 1))
- {
- this.profile_last = 0;
- this.prefs.setIntPref('profile_last', this.profile_last);
- }
- // Current profile
- var profile = profiles[this.profile_last].split('|');
-
- // Profiles in menu
- this.profiles_menu();
-
- var date = new Date();
- var date_today = date.toLocaleFormat('%Y-%m-%d');
-
- var ajax = null;
- ajax = new XMLHttpRequest();
- ajax.open('GET', 'https://www.google.com/analytics/feeds/data?ids='+profile[1]+'&metrics=ga:bounces,ga:newVisits,ga:pageviews,ga:visitors,ga:visits&start-date='+date_today+'&end-date='+date_today+'&prettyprint=true'+'&'+(new Date()).getTime(), true);
- ajax.setRequestHeader('Authorization', 'GoogleLogin '+this.token);
- ajax.setRequestHeader('GData-Version', '2');
- ajax.onload = function()
- {
- gic2.gebi('label').style.color = '#000';
-
- if (ajax.status == 200)
- {
- var xml = ajax.responseXML;
-
- var label = gic2.tpl(xml, 'label');
- var tooltip = gic2.tpl(xml, 'tooltip');
-
- // Cache
- gic2.prefs.setCharPref('cache_label', label);
- gic2.prefs.setCharPref('cache_tooltip', tooltip);
- gic2.prefs.setCharPref('cache_update', (new Date()).getTime());
-
- gic2.gebi('label').label = label;
- gic2.gebi('tooltip').getElementsByTagName('description')[0].value = tooltip;
-
- // Copy & tooltip
- gic2.gebi('menu_copy_label').label = 'Copy value "' + label + '"';
- gic2.gebi('menu_copy_tooltip').label = 'Copy title "' + tooltip + '"';
- gic2.gebi('menu_copy_label').value = label;
- gic2.gebi('menu_copy_tooltip').value = tooltip;
- if (tooltip)
- {
- gic2.gebi('menu_copy_tooltip').hidden = false;
- gic2.gebi('tooltip').hidden = false;
- }
- }
- else
- {
- // Invalid token
- if (ajax.status == 401)
- {
- gic2.auth();
- gic2.main();
- }
- // Invalid profile
- else if (ajax.status == 400)
- {
- gic2.accounts();
- gic2.main();
- }
- else
- {
- gic2.gebi('label').label = 'Error #'+ajax.status;
- }
- }
- };
- ajax.send(null);
- },
-
- profiles_menu: function()
- {
- if (!this.profiles) return;
-
- var profiles = this.profiles.split('||');
-
- // Remove
- var profiles_menu = this.gebi('profiles');
- while(profiles_menu.hasChildNodes())
- {
- profiles_menu.removeChild(profiles_menu.firstChild);
- }
-
- // Create
- const XUL_NS = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
-
- profiles_menu.appendChild(document.createElementNS(XUL_NS, 'menuseparator'));
-
- var menuitem = null;
- var profile = [];
- for (var i=0; i<profiles.length; i++)
- {
- profile = profiles[i].split('|');
- menuitem = document.createElementNS(XUL_NS, 'menuitem');
- menuitem.setAttribute('label', profile[0]);
- menuitem.setAttribute('name', 'gic2_profile');
- menuitem.setAttribute('type', 'radio');
- menuitem.setAttribute('oncommand', 'gic2.profile_set('+i+');');
- if (i == this.profile_last) menuitem.setAttribute('checked', 'true');
-
- profiles_menu.appendChild(menuitem);
- }
-
- this.gebi('profiles').hidden = false;
- },
-
- profile_set: function(profile_last)
- {
- this.profile_set_menu = true;
- this.profile_last = profile_last;
- this.main();
- },
-
- profiles_reload: function()
- {
- this.prefs.setCharPref('profiles', '');
- this.profiles = '';
- this.profile_last = 0;
- this.main();
- },
-
- shutdown: function()
- {
- this.prefs.removeObserver('', this);
- },
-
- observe: function(subject, topic, data)
- {
- if (topic != 'nsPref:changed') return;
-
- switch(data)
- {
- case 'email':
- this.token = '';
- this.profiles = '';
- this.profile_last = 0;
-
- this.prefs.setCharPref('token', '');
- this.prefs.setCharPref('profiles', '');
- this.prefs.setIntPref('profile_last', 0);
-
- this.main();
- break;
- }
- },
-
-
-
- main_click: function(event)
- {
- // Disable right click
- if (event.button != 2) this.main();
- },
-
- cache: function()
- {
- var cache_label = this.prefs.getCharPref('cache_label');
- var cache_tooltip = this.prefs.getCharPref('cache_tooltip');
-
- if (!cache_label) return false;
-
- if (((new Date()).getTime() - this.prefs.getCharPref('cache_update')) > (this.prefs.getIntPref('cache_interval') * this.prefs.getIntPref('cache_unit') * 1000)) return false;
-
- this.gebi('label').label = cache_label;
- this.gebi('tooltip').getElementsByTagName('description')[0].value = cache_tooltip;
-
- return true;
- },
-
- tpl: function(xml, tpl_name)
- {
- var tpl = this.prefs.getCharPref(tpl_name);
- if (!tpl) return '';
-
- var regexp = /<([a-z0-9_:,-]+)>/ig;
- return tpl.replace(regexp, function(str, match)
- {
- if (!match) return;
-
- var tpl_parts = match.split(',');
- var tpl_parts_c = tpl_parts.length;
-
- if (tpl_parts_c > 3) return '<'+match+'>';
-
- // Clean
- if (!(/^[0-9]+$/.test(tpl_parts[tpl_parts_c-1])))
- {
- if (tpl_parts_c == 3) return '<'+match+'>';
- else
- {
- tpl_parts.push(1);
- tpl_parts_c++;
- }
- }
-
- var value = '';
- try
- {
- if (tpl_parts_c == 3)
- {
- value = xml.getElementsByTagName(tpl_parts[0]).item(tpl_parts[2]-1).getAttribute(tpl_parts[1]);
- }
- else
- {
- value = xml.getElementsByTagName(tpl_parts[0]).item(tpl_parts[1]-1).firstChild.data;
- }
- }
- catch(e) {}
-
- if (value) return value;
- else return '<'+match+'>';
- });
- },
- dialog: function(name)
- {
- window.openDialog('chrome://gic'+this.id+'/content/'+name+'.xul', 'gic'+this.id+'_'+name, 'chrome, centerscreen, modal');
- },
- copy: function(str)
- {
- Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper).copyString(str);
- },
- gebi: function(name)
- {
- return document.getElementById('gic'+this.id+'_'+name);
- },
- web: function(uri)
- {
- gBrowser.selectedTab = gBrowser.addTab('http://getinformer.com'+uri);
- }
- }
-
- window.addEventListener('pageshow', function(e) {gic2.startup();}, false);
- window.addEventListener('unload', function(e) {gic2.shutdown();}, false);